package it.asvp.asvp_portal.service.util;

import it.asvp.asvp_portal.asvp_util.enumerative.RoleName;
import it.asvp.asvp_portal.data.entity.member.Role;

public final class Permission {
    //create,update,read,delete
	final static boolean[][] memberPermission={{false,false,true,false},{false,false,false,false},{false,false,false,false},{false,false,false,false}};
	final static boolean[][] councilorPermission={{true,true,true,false},{false,false,true,false},{false,false,true,false},{false,false,true,false}};
	final static boolean[][] presidentPermission={{true,true,true,true},{true,true,true,true},{true,true,true,true},{false,false,true,false}};		
	final static boolean[][] adminPermission={{true,true,true,true},{true,true,true,true},{true,true,true,true},{true,true,true,true}};
		
	public static boolean checkPermission(Role role1,Role role2,Operation operation) throws Exception{
	  RoleName roleName1=role1.getRoleName();
	  RoleName roleName2=role2.getRoleName();
	  
	  if(roleName1.ordinal()<0 || roleName1.ordinal()>3) throw new Exception("permessi non validi");
	  if(roleName2.ordinal()<0 || roleName2.ordinal()>3) throw new Exception("permessi non validi");
	  
	  switch(roleName1.ordinal()){
		
		case 0: { //member
			return memberPermission[roleName2.ordinal()][operation.ordinal()];
		}
		case 1: {  //councilor
			return councilorPermission[roleName2.ordinal()][operation.ordinal()];
		}
		case 2: {  //president
			return presidentPermission[roleName2.ordinal()][operation.ordinal()];
		}
		case 3: {  //admin
			return adminPermission[roleName2.ordinal()][operation.ordinal()];
		}
		default: return false;
	  }
		
	}
}
